package com.ruoyi.file.dobbo;

import com.ruoyi.common.tools.utils.file.FileUtils;
import com.ruoyi.common.resp.domain.R;
import com.ruoyi.file.service.ISysFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.file.api.dobbo.RemoteFileService;
import com.ruoyi.file.api.domain.SysFile;

/**
 * 提供能力
 */
@Slf4j
@DubboService
@Service
public class RemoteFileServiceImpl implements RemoteFileService {

    @Autowired
    private ISysFileService sysFileService;

    @Override
    public R<SysFile> upload(MultipartFile file) {
        try {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("上传文件失败" , e);
            return R.fail(e.getMessage());
        }
    }
}
